using System;
using System.Collections.Generic;
using System.Text;
using QLTDK.DataAccess.Interfaces;
using QLTDK.Entities;
using QLTDK.Utilities;

using Spring.Transaction.Interceptor;

namespace QLTDK.Controller
{
    public interface ICtrCongTrinhCtrl
    {
        CtrCongtrinh GetByTenCongTrinh(String tenCongTrinh);
        TdkTudieukhien GetTDK(String tenCongTrinh);
    }

    public class CtrCongTrinhCtrl : BaseCtrl, ICtrCongTrinhCtrl
    {
        //public Boolean Insert(CongTrinh congTrinh)
        //{
        //    ICongTrinhDao congTrinhDao = (ICongTrinhDao)ContextRegistry.GetContext().GetObject("CongTrinhDao");
        //    // Neu chua ton tai ten cong trinh thi moi insert vao.
        //    try
        //    {
        //        if (congTrinhDao.IsExist(congTrinh.TenCongTrinh) == false)
        //        {
        //            congTrinhDao.Save(congTrinh);
        //            return true;
        //        }
        //        else
        //            return false;
        //    }
        //    catch(Exception ex)
        //    {
        //        throw ex;
        //    }
        //}

        [Transaction(ReadOnly=true)]
        public CtrCongtrinh GetByTenCongTrinh(String tenCongTrinh)
        {
            ICtrCongtrinhDao congTrinhDao = (ICtrCongtrinhDao)ApplicationContext.GetObject("CtrCongTrinhDao");
            CtrCongtrinh ctr = congTrinhDao.GetByTenCongTrinh(tenCongTrinh)[0];
            return ctr;
        }

        [Transaction(ReadOnly = true)]
        public TdkTudieukhien GetTDK(String tenCongTrinh)
        {
            ICtrCongtrinhDao congTrinhDao = (ICtrCongtrinhDao)ApplicationContext.GetObject("CtrCongTrinhDao");
            CtrCongtrinh ctr = congTrinhDao.GetByTenCongTrinh(tenCongTrinh)[0];

            return ctr.TdkTudieukhiensMaCongTrinhLapDat[0];
        }
    }
}
